package com.shelpe.services.lexicon.combine.rule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shelpe.services.lexicon.constants.Origin;
import com.shelpe.services.lexicon.entity.AdgroupRecord;
import com.shelpe.services.lexicon.entity.KeywordRecommend;
import com.shelpe.services.lexicon.po.Category;
import com.shelpe.services.lexicon.po.Product;
import com.shelpe.services.lexicon.repository.jpa.CategoryRepository;
import com.shelpe.services.lexicon.repository.jpa.ProductRepository;
import com.shelpe.services.lexicon.repository.mongo.KeywordRecommendRepository;
import com.shelpe.services.lexicon.simba.CategoryClient;
import com.shelpe.services.lexicon.simba.CategoryClient.CategoryDto;
import com.shelpe.services.lexicon.simba.CategoryClient.CategoryListRespone;
import com.shelpe.services.lexicon.suanzi.SuanziClient;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ProductCombineRule implements CombineRule {

	@Autowired
	private SuanziClient suanziClient;
	@Autowired
	private CategoryClient categoryClient;
	@Autowired
	private CategoryRepository categoryRepository;
	@Autowired
	private ProductRepository productRepository;
	@Autowired
	private KeywordRecommendRepository keywordRecommendRepository;
	
	@Override
	public List<KeywordRecommend> combine(AdgroupRecord record) {
		String productName = record.getSource().getItemName();
		String leafCategoryName = null;
		List<String> propWords = new ArrayList<String>();
		if(record.getCategoryIDs() != null && record.getCategoryIDs().length > 0){
			long catID = record.getCategoryIDs()[record.getCategoryIDs().length - 1];
			Category cat = this.categoryRepository.findOne(catID);
			if(cat == null){
				this.syncCategories(record);
			}
			 cat = this.categoryRepository.findOne(catID);
			 
			 if(cat != null){
				 log.debug("category is found: {}", cat.getName());
				 leafCategoryName = cat.getName();
			 }
		}
		
		if(record.getItemID() > 0){
			Product prod = this.productRepository.findOne(record.getItemID());
			if(prod != null){
				productName = prod.getProductName();
				
				log.debug("propperty is found: {}", prod.getPropertyNames());
				if(prod.getPropertyNames() != null){
					String[] props = prod.getProductName().split(";");
					for(String prop: props){
						if(prop != null && prop.lastIndexOf(":") > -1){
							propWords.add(prop.substring(prop.lastIndexOf(":"), prop.length()));
						}
					}
				}
			}
		}
		
		
		List<String> prodWords = new ArrayList<String>();
		if(StringUtils.isNotBlank(productName)){
			String res = this.suanziClient.splitWords(productName);
			log.debug("split product name from suanzi: {}", res);
			if(StringUtils.isNotBlank(res)){
				String[] words = res.split("\\|\\|");
				for(String word: words){
					if(StringUtils.isNotBlank(word)){
						prodWords.add(word);
					}
				}
			}
		}
		List<String> catWords = new ArrayList<String>();
		if(StringUtils.isNotBlank(leafCategoryName)){
			String res  = this.suanziClient.splitWords(leafCategoryName);
			log.debug("split category name from suanzi: {}", res);
			if(StringUtils.isNotBlank(res)){
				String[] words = res.split("\\|\\|");
				for(String word: words){
					if(StringUtils.isNotBlank(word)){
						catWords.add(word);
					}
				}
			}
		}
		
		
		List<String> words = new ArrayList<String>();
		for(String catWord: catWords){
			for(String prodWord: prodWords){
				String w = catWord + " " +prodWord;
				log.debug("combine word: {}", w);
				words.add(w);
			}
		}
		
		if(record.getSource().getAttributeNames() != null && record.getSource().getAttributeNames().size() > 0){
			for(String catWord: catWords){
				for(String prodWord: prodWords){
					for(String attrWord: propWords){
						String w = catWord + " " +prodWord + " " + attrWord;
						log.debug("combine word: {}", w);
						words.add(w);
					}
				}
			}
		}
		
		
		List<KeywordRecommend> manualRecommends = new ArrayList<KeywordRecommend>();
		for(String word: words){
			KeywordRecommend recommend = new KeywordRecommend();
			recommend.setOrigin(Origin.combine);
			recommend.setAdgroupID(record.getAdgroupID());
			recommend.setAdgroupRecordID(record.getRecordID());
			recommend.setWord(word);
			recommend.setWeight(81);
			
			recommend.setCreateTime(new Date());
			recommend.setLastModifiedTime(new Date());
			manualRecommends.add(recommend);
		}
		this.keywordRecommendRepository.save(manualRecommends);
		
		return manualRecommends;
	}

	
	private void syncCategories(AdgroupRecord record){
		String[] catIDs = new String[record.getCategoryIDs().length];
		for(int i=0;i<record.getCategoryIDs().length;i++){
			catIDs[i] = record.getCategoryIDs()[i] + "";
		}
		CategoryListRespone res = this.categoryClient.findCategory(catIDs, 1, record.getTanentID());
		if(res != null && res.getRes() != null && res.getRes().getDataList() != null && res.getRes().getDataList().getCategories() != null){
			for(CategoryDto dto: res.getRes().getDataList().getCategories()){
				Category po = new Category();
				BeanUtils.copyProperties(dto, po);
				this.categoryRepository.save(po);
			}
		}
	}
}
